package com.sudo.service.admin.mail;

import com.sudo.common.component.TemplateUtil;
import com.sudo.common.enums.SysJobAction;
import com.sudo.dao.admin.entity.SysJobLog;
import com.sudo.dao.admin.model.vo.StsBehaviorVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 构建邮件模板
 */
@Component
public class MailPojoUtil {

    /**
     * 注意： 静态成员变量不能@Resource直接属性注入，所以可以采用构造注入
     */
    private static TemplateUtil templateUtil;

    @Resource
    public void setTemplateUtil(TemplateUtil templateUtil){
        MailPojoUtil.templateUtil = templateUtil;
    }

    // 任务调度日志邮件（如果失败，则发邮件提醒）
    public static MailPojo methodJobExceptionMail(String mailAddress, SysJobLog sysJobLog){
        MailPojo mailPojo = new MailPojo();
        String sysJobAction = SysJobAction.valueOf(sysJobLog.getJobAction()).value;
        String title = "任务【" + sysJobLog.getJobId() + "】["+ sysJobAction +"]运行失败：" + sysJobLog.getJobNote();
        mailPojo.setTitle(title);
        StringBuilder text = new StringBuilder();
        if (StringUtils.isNotBlank(sysJobLog.getJobServer())) {
            text.append("【节点】：" + sysJobLog.getJobServer()).append("<br/>");
        }else {
            text.append("【节点】：本地服务").append("<br/>");
        }
        text.append("【任务】：" + sysJobLog.getJobId()).append("<br/>");
        text.append("【模块】：" + sysJobLog.getJobBean()).append("<br/>");
        text.append("【方法】："+ sysJobLog.getJobMethod()).append("<br/>");
        if(StringUtils.isNotBlank(sysJobLog.getJobParams())){
            text.append("【参数】："+ sysJobLog.getJobParams()).append("<br/>");
        }else {
            text.append("【参数】：无").append("<br/>");
        }
        text.append("【耗时】：" + sysJobLog.getJobUseTime()).append("ms").append("<br/>");
        text.append("【运行时间】：" + sysJobLog.getJobStartTime()).append("<br/>");
        text.append("【结束时间】：" + sysJobLog.getJobEndTime()).append("<br/>");
        text.append("【异常说明】：" + sysJobLog.getJobNote()).append("<br/>");
        text.append("【异常详情】：" + sysJobLog.getJobNoteDesc()).append("<br/>");

        mailPojo.setText(text.toString());
        String[] mailSendArr = mailAddress.split(",");
        mailPojo.setSentTo(mailSendArr);
        return mailPojo;
    }

    // 用户注册，邮箱验证码
    public static MailPojo emailVerifyCode(String emailAddress, String code) {
        MailPojo mailPojo = new MailPojo();
        mailPojo.setTitle("JBuss验证码");
        mailPojo.setText("欢迎使用JBuss，您的验证码是：" + code);
        String[] mailSendArr = emailAddress.split(",");
        mailPojo.setSentTo(mailSendArr);
        return mailPojo;
    }

    // 用户行为邮件
    public static MailPojo stsUserBehaviorMail(String mailAddress, List<StsBehaviorVO> stsBehaviorVOList,
                                               LocalDate minDate,LocalDate maxDate) {
        MailPojo mailPojo = new MailPojo();
        String date = "[" + minDate.toString() + "至" + maxDate.toString() + "]";
        mailPojo.setTitle(date + "用户行为统计分析");
        StringBuilder emailBody = new StringBuilder();
        emailBody.append("<table>\n");
        emailBody.append("   <tr>\n");
        emailBody.append("       <td>日期</td>\n");
        emailBody.append("       <td>登录人数</td>\n");
        emailBody.append("       <td>登录人次</td>\n");
        emailBody.append("       <td>活跃度</td>\n");
        emailBody.append("   </tr>\n");
        for (StsBehaviorVO be : stsBehaviorVOList) {
            emailBody.append("<tr>\n");
            emailBody.append("    <td>").append(be.getDate()).append("</td>\n");
            emailBody.append("    <td>").append(be.getLoginNop()).append("</td>\n");
            emailBody.append("    <td>").append(be.getLoginNot()).append("</td>\n");
            emailBody.append("    <td>").append(be.getNumBehavior()).append("</td>\n");
            emailBody.append("</tr>");
        }
        emailBody.append("</table>\n");
        String emailHeader = date + "用户行为统计分析";
        String wholeHtml = templateUtil.getEmailTemplate(emailHeader,emailBody.toString());
        mailPojo.setText(wholeHtml);
        String[] mailSendArr = mailAddress.split(",");
        mailPojo.setSentTo(mailSendArr);
        return mailPojo;
    }
}
